﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReceiptTracker
{
    public partial class FormEditReceipt : Form
    {
        ContextMenuStrip strip = new ContextMenuStrip();
        ToolStripMenuItem edit = new ToolStripMenuItem("Edit");
        ToolStripMenuItem delete = new ToolStripMenuItem("Delete");
        public bool ValuesChanged { get; set; }
        private SQLiteDB db;
        private DataTable dt = new DataTable();
        public FormEditReceipt(SQLiteDB db)
        {
            InitializeComponent();
            this.db = db;
            RefreshView();
            dataGridView.DataSource = dt;
            strip.Items.Add(edit);
            strip.Items.Add(delete);
            edit.Click += new EventHandler(this.edit_Clicked);
            delete.Click += new EventHandler(this.delete_Clicked);
        }

        private void RefreshView()
        {
            db.SQLQuery("SELECT Receipts.id,date,spent,Shops.name AS Shop,Categories.name AS Category FROM Receipts,Shops,Categories WHERE Receipts.shop= Shops.id AND Shops.category=Categories.Id;");
            dt.Clear();
            dt.Merge(db.QueryResult);
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            RefreshView();
        }

        private void FormEditReceipt_Load(object sender, EventArgs e)
        {
            ValuesChanged = false;
        }

        private void dataGridView_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
        {
            e.ContextMenuStrip = strip;
        }

        private void edit_Clicked(object sender, EventArgs e)
        {
            
            FormInputValue FormInputValue = new FormInputValue("Edit", "Please enter a new value:");
            if (dataGridView.SelectedCells[0].ColumnIndex < 4)
            {
                FormInputValue.ShowDialog();
                if (FormInputValue.DialogResult == DialogResult.OK && MessageBox.Show("Really change " +
                        dataGridView.SelectedCells[0].Value + " to " + FormInputValue.Input, "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string column = dataGridView.SelectedCells[0].OwningColumn.Name.ToLower();
                    dataGridView.Rows[dataGridView.SelectedCells[0].RowIndex].Selected = true; //Select the entire row
                    if (column != "shop" && column != "category")
                    {
                        db.SQLCommand("UPDATE Receipts SET " + column + "=" + FormInputValue.Input + " WHERE id=" + dataGridView.SelectedRows[0].Cells["id"].Value + ";");
                        RefreshView();
                        ValuesChanged = true;
                    }
                    else if (column == "shop")
                    {
                        db.SQLQuery("SELECT id,name FROM Shops;");
                        bool match = false;
                        int id = 0;
                        foreach (DataRow row in db.QueryResult.Rows)
                        {
                            if (FormInputValue.Input == row["name"].ToString())
                            {
                                match = true;
                                id = Convert.ToInt32(row["id"]);
                            }
                        }
                        if (match)
                        {
                            db.SQLCommand("UPDATE Receipts SET shop=" + id.ToString() + " WHERE id=" + dataGridView.SelectedRows[0].Cells["id"].Value + ";");
                            RefreshView();
                            ValuesChanged = true;
                        }
                        else
                        {
                            MessageBox.Show(FormInputValue.Input + " is not a valid shop...", "Invalid shop", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }//end-if shop column
                }//end-if really change value         
            }
            else
            {
                MessageBox.Show("Cannot change a shops category from this window", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void delete_Clicked(object sender, EventArgs e)
        {
            dataGridView.Rows[dataGridView.SelectedCells[0].RowIndex].Selected = true; //Select the entire row
            if (MessageBox.Show("Do you really want to delete the receipt:\n" + dataGridView.SelectedRows[0].Cells["date"].Value + " spent " +
                dataGridView.SelectedRows[0].Cells["spent"].Value + " kr at " + dataGridView.SelectedRows[0].Cells["Shop"].Value, "Delete receipt", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                db.SQLCommand("DELETE FROM Receipts WHERE id=" + dataGridView.SelectedRows[0].Cells["id"].Value + ";");
                db.SQLCommand("UPDATE Receipts SET id=id-1 WHERE id>" + dataGridView.SelectedRows[0].Cells["id"].Value + ";");
                RefreshView();
                ValuesChanged = true;
            }
        }
    }
}
